#include "Common/Algebra/Vec2.h"

namespace cmn {

	Vec2 Vec2::operator += ( const Vec2& v ) {
		X += v.X;
		Y += v.Y;
		return *this;
	}

	Vec2 Vec2::operator -= ( const Vec2& v ) {
		X -= v.X;
		Y -= v.Y;
		return *this;
	}

	Vec2 Vec2::operator *= ( float a ) {
		X *= a;
		Y *= a;
		return *this;
	}

	Vec2 Vec2::operator /= ( float a ) {
		X /= a;
		Y /= a;
		return *this;
	}

	Vec2 Vec2::Normalize() {
		float len = Length();
		X /= len;
		Y /= len;
		return *this;
	}

	Vec2 Vec2::RotateByAngle( float angle ) {
		return RotateToAngle( GetAngle() + angle );
	}

	Vec2 Vec2::RotateToAngle( float angle ) {
		float len = Length();
		X = cos( angle ) * len;
		Y = sin( angle ) * len;
		return *this;
	}

	Vec2 Vec2::RotateLeft() {
		float x = X;
		X = -Y;
		Y = x;
		return *this;
	}

	Vec2 Vec2::RotateRight() {
		float x = X;
		X = Y;
		Y = -x;
		return *this;
	}

	float Vec2::GetAngle() const {
		float rot = atan2( Y, X );
		if( rot < 0.0 )
			return rot + 2.0f * Pi;
		return rot;
	}

	Vec2 Vec2::GetNormal() const {
		float len = Length();
		return Vec2( X / len, Y / len );
	}

	Vec2 Vec2::GetRotationByAngle( float angle ) const {
		return GetRotationToAngle( GetAngle() + angle );
	}

	Vec2 Vec2::GetRotationToAngle( float angle ) const {
		float len = Length();
		return Vec2( cos( angle ) * len, sin( angle ) * len );
	}

	Vec2 Vec2::GetLeftRotation() const {
		return Vec2( -Y, X );
	}

	Vec2 Vec2::GetRightRotation() const {
		return Vec2( Y, -X );
	}
}
